import Markdown from 'markdown-it'

const mdOptions = {
  linkify: false,
  typographer: true,
  breaks: true,
  html: true
}

let mdHandle = new Markdown(mdOptions)
// 自定义渲染规则 a标签添加_blank属性
mdHandle.renderer.rules.link_open = function (tokens: any, idx: any, options: any, env: any, self: any) {
  const aIndex = tokens[idx].attrIndex('target')

  if (aIndex < 0) {
    tokens[idx].attrPush(['target', '_blank']) // 添加新属性
  } else {
    // 如果已存在，确保它的值是'_blank'
    const aValue = tokens[idx].attrs[aIndex]
    if (aValue[1] !== '_blank') {
      tokens[idx].attrs[aIndex][1] = '_blank'
    }
  }

  // 使用默认的处理方式
  return self.renderToken(tokens, idx, options)
}

export const md = mdHandle
